#include <Arduino.h>
#include <Wire.h>

#ifndef DS1307_h
#define DS1307_h

#define DeviceAddressID         0x68  //address of DS1307
#define DeviceStopClock        0x80  // StopBit
#define DeviceStartClock       0x7f  // StartBit

#define SqwDisable              0x00
#define SqwRate1		0x10
#define SqwRate4k		0x11
#define SqwRate8k		0x12
#define SqwRate32k	        0x13

#define dsRecive7Byte           0x07
#define index0                  0x00
#define index7                  0x07  

class Ds1307
{
private:
  boolean deviceResponded;
  boolean deviceClockWork;
  
  uint8_t second;
  uint8_t minute;
  uint8_t hour;
  uint8_t weekDay;
  uint8_t day;
  uint8_t month;
  uint8_t year;

  uint8_t controlDat;          //This variable contains the number of second and control data
  uint16_t errorData;
  
  uint8_t dec2bcd(uint8_t num);
  uint8_t bcd2dec(uint8_t num);
  
  void checkDeviceClockWork(uint8_t);
  void beginI2CtoDS1307(void);
  boolean checkIfConnectionWorked(void); 
  boolean checkIfDeviceWillRespond(void);
  
  void stopDeviceClock(void);
  void startDeviceClock(void);
  void writeDataToDS1307(void);
  void checkErrorData(uint8_t);

public:

  void initialize(void);    //enable wire //static ??
  boolean isDeviceResponded(void);
  boolean isDeviceClockWork(void);
  
  void stopDevice(void);
  void startDevice(void);
  
  byte getYear(void);
  byte getMonth(void);
  byte getDay(void);
  byte getWeekDay(void);
  byte getHour(void);
  byte getMinute(void);
  byte getSecond(void);
  byte getControlData(void);
  byte getErrorCounter(void);
  
  void setYear(uint8_t);
  void setMonth(uint8_t);
  void setDay(uint8_t);
  void setWeekDay(uint8_t);
  void setHour(uint8_t);
  void setMinute(uint8_t);
  void setSecond(uint8_t);
 

  void readData(void);
  void setData(void);
  
  void setSqwOutput(uint8_t);

};

#endif

